##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Table 4: Agricultural Tariffs and Subsidies Average Treatment Effects

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################

library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(plyr)
library(scales)

##################################################################

rm(list=ls())

##################################################################
# Treatment Effects: Agricultural Trade Protections
##################################################################

data.working <- readRDS("d_r_cleaned_analysis_dataset.Rda")

dep.var <- c("q11","q11.yes.nodk", "q11.yes.petition")

ind.group <- c("group3" , "group2")

reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
matrix <- vector("list", length(dep.var))

x=0 

for(i in 1:length(dep.var)){
	
	my.formula <-paste(dep.var[i],'~', paste(ind.group, collapse= ' + '))
	temp.model <- lm(my.formula, data=data.working)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	x = x+1
	se.models[[x]] <- temp.se
	reg.models[[x]] <- temp.model

}

varlabels <-       c("Information", "Salience", "Constant (Control Mean)" )
spectitle <- c("Support for Reductions in US  Agricultural Tariffs and Subsidies")
outputfile <- c("tf_t_04_ate_agricultural.txt")
columnlabels <-c("Ordinal (1-4)", "Supports Reductions (1/0)", "Petition (1/0)")
depvarcaption <- c("Support for Reductions in US  Agricultural Tariffs and Subsidies")
cutoffs.star <- c(0.1, 0.02)
char.star = c("**", "***")

data.subset <- data.working[data.working$group2==1 | data.working$group3==1,] 
tresults.1 <- round((t.test(q11~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.2 <- round((t.test(q11.yes.nodk~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.3 <- round((t.test(q11.yes.petition~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
lines.add <- list(c("Information-Salience (p-val.)", tresults.1, tresults.2, tresults.3))

stargazer(reg.models[[1]], reg.models[[2]], reg.models[[3]],
          se=list(se.models[[1]], se.models[[2]], se.models[[3]]),
         type="text",
         title= spectitle,
          out=outputfile,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=TRUE,
          digits=2,
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = columnlabels,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
                  star.cutoffs = cutoffs.star,
     star.char = char.star,
         add.lines=lines.add
)   